package com.jerry.mingxing.ui.store;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jerry.mingxing.R;
import com.jerry.mingxing.bean.ShowBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/1.
 */
public class ShowAdapter extends BaseAdapter {
    private List<ShowBean.DataBean.ItemsBean> totallist;
    private Context context;
    private LayoutInflater inflater;

    public ShowAdapter(List<ShowBean.DataBean.ItemsBean> totallist, Context context) {
        this.totallist = totallist;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return totallist.size();
    }

    @Override
    public Object getItem(int position) {
        return totallist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mholder=null;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.storefragment_show_item, parent, false);
            mholder=new ViewHolder(convertView);
            convertView.setTag(mholder);
        }else {
            mholder= (ViewHolder) convertView.getTag();
        }
        if (totallist.get(position).getComponent().getPrice()!=null){
        mholder.storefragmentShowItemSdv.setImageURI(Uri.parse(totallist.get(position).getComponent().getPicUrl()));
        mholder.storefragmentShowItemTitle.setText(totallist.get(position).getComponent().getDescription());


            mholder.storefragmentShowItemPrice.setText(totallist.get(position).getComponent().getPrice()+"元");
            mholder.storefragmentShowItemOldprice.setText("原价"+totallist.get(position).getComponent().getOrigin_price() +"元");

        }

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.storefragment_show_item_sdv)
        SimpleDraweeView storefragmentShowItemSdv;
        @Bind(R.id.storefragment_show_item_title)
        TextView storefragmentShowItemTitle;
        @Bind(R.id.storefragment_show_item_price)
        TextView storefragmentShowItemPrice;
        @Bind(R.id.storefragment_show_item_oldprice)
        TextView storefragmentShowItemOldprice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    public void reloadData(List<ShowBean.DataBean.ItemsBean> list) {

        if (list.get(0).getComponent().getPrice()==null){
            list.remove(0);
        }

        totallist=list;
        notifyDataSetChanged();
    }
}
